<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr,
		errorModal 
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				addressCacheKey: "hr-kq-businessleave-data-info-address-cache",
				departureCityCacheKey:"hr-kq-businessleave-data-info-departureCity-cache",
				InstCacheKey:"hr-kq-businessleave-data-info-inst-cache",
				userId: '',
				editId:'',
				objStr:'',
				subIndex:'',
				partakeInstCache:'hr-kq-businessleave-data-info-partakeInst-cache',
				feesAggregatedWayCache:'hr-kq-businessleave-data-info-feesAggregatedWay-cache',
				partakeUserCache:'hr-kq-businessleave-data-info-partakeUser-cache',
				researchDevelopmentCache:'hr-kq-businessleave-data-info-researchDevelopment-cache',
				businessOpportunityCache:'hr-kq-businessleave-data-info-businessOpportunity-cache',
				marketingPlanningProductCache:'hr-kq-businessleave-data-info-marketingPlanningProduct-cache',
				saleOrderCache:'hr-kq-businessleave-data-info-saleOrder-cache',
				newProductCache:'hr-kq-businessleave-data-info-newProduct-cache',
				sameOutUserCacheKey:'hr-kq-businessleave-data-info-sameOutUser-cache',
				defaultDataCache:'hr-kq-businessleave-data-info-defaultData-cache',
				businessTripTypeId:'',
				businessTripTypeName:'',
				businessTripTypeCode:'',
				oldAffairType:'',
				leaveCountryList: [],
				oldLectLeaveCountryCode: '',
			})
		},
		methods:{
			initload(){
				let _that = this
				let pro1 = _that.om.request("dictZero/getListByCode",{code:'37373'})
				let pro2 = _that.om.request("tsysdict/getListByCode",{code:'38802'})
				let pro3 = _that.om.request("tsysdict/getListByCode",{code:'38803'})
				let pro4 = _that.om.request("dictZero/getListByCode",{code:'39414'})
				Promise.all([pro1,pro2,pro3,pro4]).then(res=>{
					_that.$refs.autoform.changeEditor('workOfficeInfo', 'vehicleCode', 'data', configSelectData(res[0], 'code','dicname'))
					_that.$refs.autoform.initSelectIndex('workOfficeInfo', 'vehicleCode')
					_that.$refs.autoform.changeEditor('workOfficeInfo', 'affairTypeId', 'data', configSelectData(res[1], 'id','dicname'))
					_that.$refs.autoform.initSelectIndex('workOfficeInfo', 'affairTypeId')
					_that.$refs.autoform.changeEditor("workOfficeInfo","oneWayRoundTripId","data",configSelectData(res[2],"id",'dicname'))
					_that.$refs.autoform.initSelectIndex('workOfficeInfo', 'oneWayRoundTripId')
					_that.$refs.autoform.changeEditor('workOfficeInfo', 'leaveCountryId', 'data', configSelectData(res[3], 'id','dicname'))
					_that.$refs.autoform.initSelectIndex('workOfficeInfo', 'leaveCountryId')
					_that.leaveCountryList = res[3]
				})
			},
			verifyActualEndDate(date){
				let _that = this
				var standDate = new Date();
				standDate.setDate(standDate.getDate() - _that.reportDay - 1);
				var d = new Date(date);
				if(_that.reportDay && d.getTime() < standDate.getTime()){
					return "管控参数配置中做了约束，“实际完成日期”只能选择当前日期、或当前日期之前 " + _that.reportDay + " 天内的日期，请重新编辑！";
				}
				return true
			},
			onSelectStartTime(item){
				let _that = this 
				_that.$refs.autoform.clear("workOfficeInfo","endTime")
			},
			clearPartakeInstId(){
				let _that = this
				_that.$refs.autoform.clear('workOfficeInfo', 'partakeInstId');
				_that.$refs.autoform.clear('workOfficeInfo', 'feesAggregatedWayCode');
				_that.$refs.autoform.changeEditor("workOfficeInfo", "partakeUserId", "required", false)
			},
			verifyPartakeUser(){
				let _that = this
				if(!_that.$refs.autoform.formdata.partakeInstId){
					let title1 = _that.$refs.autoform.getEditorTitle("workOfficeInfo")
					let title2 = _that.$refs.autoform.getEditorTitle("workOfficeInfo","partakeInstId")
					errorModal(_that,'提示信息',title1+'的'+title2+'需要优先完成编辑!',()=>{
						return false
					})
				}else{
					_that.$refs.autoform.changeEditor("workOfficeInfo","partakeUserId",{
						"link":'../../public/search/search',
						"linkParam":{
							other:JSON.stringify({
								instId:_that.$refs.autoform.formdata.partakeInstId
							}),
							url:'orgInstPosUserOfficial/getUserListByInstPosId',
							initload:true,
							namefield:'username',
							cachename:_that.partakeUserCache
						}
					})
					return true
				}
			},
			onSelectActualEndDate(date){
				let _that = this
					let arr = JSON.parse(_that.objStr)
					let jarr = []
					for(let i in arr){
						arr[i].startTime += ':00'
						arr[i].endTime += ':00' 
						if(_that.subIndex != _that.autoformdata[0].order){
							jarr.push(arr[i])
						}
					}
					if( _that.$refs.autoform.formdata.startTime){
						_that.om.request('tworkoffice/isTimeOverlay',{
							'start': _that.$refs.autoform.formdata.startTime+":00",
							'end':date+":00",
							'userId': _that.userId,
							'editId': _that.editId,
							'objStr':JSON.stringify(jarr)
						}).then(res=>{
							if(!res.succeed){
								_that.om.toast(res.msg,3000)
								_that.$refs.autoform.clear("workOfficeInfo","endTime")
							}
						})
					}
			},
			onselectLeaveCountry(data){
				let _that = this
				uni.setStorageSync('omBusinessTripType', data.code)
				_that.getChangeLeaveCountry(data.code)
			},
			getChangeLeaveCountry(code){
				let _that = this
				if(_that.oldLectLeaveCountryCode != code){
					if(!_that.oldLectLeaveCountryCode){
						_that.oldLectLeaveCountryCode = "39415"
					}
					if(!code){
						code = "39415"
					}
					
					if(_that.oldLectLeaveCountryCode == "39415" && code == "39416"){
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39415" && code == "39417"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
					}else if(_that.oldLectLeaveCountryCode == "39415" && code == "39418"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39416" && code == "39415"){
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39416" && code == "39417"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39416" && code == "39418"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
					}else if(_that.oldLectLeaveCountryCode == "39417" && code == "39415"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
					}else if(_that.oldLectLeaveCountryCode == "39417" && code == "39416"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39417" && code == "39418"){
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39418" && code == "39415"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else if(_that.oldLectLeaveCountryCode == "39418" && code == "39416"){
						_that.$refs.autoform.clear('workOfficeInfo', 'departureCityId');
					}else if(_that.oldLectLeaveCountryCode == "39418" && code == "39417"){
						_that.$refs.autoform.clear('workOfficeInfo', 'addressId');
					}else{
						
					}
					
					_that.oldLectLeaveCountryCode = code
				}
				
				_that.$refs.autoform.changeEditor('workOfficeInfo', 'departureCityId', {
					"link":'../../public/pageSearch/pageSearch',
					"linkParam":{
						other: JSON.stringify({
							onlyChinaFlag: code == '39415' || code == '39416' ? true : false
						}),
						searchfield: 'q',
						namefield: "dicname",
						notefield: "fullName",
						url: "regions/findCountryTreeList",
						cachename: _that.departureCityCacheKey,
						placeholder: '请输入(如:北京/beijing/bj)',
					}
				})
				
				_that.$refs.autoform.changeEditor('workOfficeInfo', 'addressId', {
					"link":'../../public/pageSearch/pageSearch',
					"linkParam":{
						other: JSON.stringify({
							onlyChinaFlag: code == '39415' || code == '39417' ? true : false
						}),
						searchfield: 'q',
						namefield: "dicname",
						notefield: "fullName",
						url: "regions/findCountryTreeList",
						cachename: _that.addressCacheKey,
						placeholder: '请输入(如:北京/beijing/bj)',
					}
				})
			},
			clearLeaveCountry(){
				let _that = this
				_that.$refs.autoform.clear("workOfficeInfo","leaveCountryId")
				if(_that.businessTripTypeId){
					for(let i=0; i<_that.leaveCountryList.length; i++){
						if(_that.leaveCountryList[i].id == _that.businessTripTypeId){
							uni.setStorageSync('omBusinessTripType', _that.leaveCountryList[i].code)
							_that.getChangeLeaveCountry(_that.leaveCountryList[i].code)
						}
					}
				}else{
					uni.setStorageSync('omBusinessTripType', '39415')
					_that.getChangeLeaveCountry('39415')
				}
			},
			onselectAffairType(data){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				formdata.affairTypeId = data.id
				formdata.affairTypeName = data.dicname
				if(!_that.oldAffairType){
					formdata.leaveCountryId = _that.businessTripTypeId
					formdata.leaveCountryName = _that.businessTripTypeName
					_that.$refs.autoform.initSelectIndex('workOfficeInfo', 'leaveCountryId')
					_that.oldAffairType = data.id
					uni.setStorageSync('omBusinessTripType', _that.businessTripTypeCode)
					_that.getChangeLeaveCountry(_that.businessTripTypeCode)
				}
			}
		},
		onLoad(e) {
			let _that = this
			_that.userId = e.userId
			_that.editId = e.editId
			_that.objStr = e.objStr
			_that.businessTripTypeId = e.businessTripTypeId
			_that.businessTripTypeName = e.businessTripTypeName
			_that.businessTripTypeCode = e.businessTripTypeCode
			_that.subIndex = (Number(e.index)+1)
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							
							changeEditor(_that.autoformdata[0].editors, 'addressId','type','areaoutsidebox')
							changeEditor(_that.autoformdata[0].editors, 'addressId', 'businessTripType', '2')
							changeEditor(_that.autoformdata[0].editors, 'addressId', 'link', '../../public/pageSearch/pageSearch')
							changeEditor(_that.autoformdata[0].editors, 'addressId', 'linkParam', {
								other: JSON.stringify({
									onlyChinaFlag: true
								}),
								searchfield: 'q',
								namefield: "dicname",
								notefield: "fullName",
								url: "regions/findCountryTreeList",
								cachename: _that.addressCacheKey,
								placeholder: '请输入(如:北京/beijing/bj)'
							})
							changeEditor(_that.autoformdata[0].editors, 'departureCityId','type','areaoutsidebox')
							changeEditor(_that.autoformdata[0].editors, 'departureCityId', 'businessTripType', '1')
							changeEditor(_that.autoformdata[0].editors, 'departureCityId', 'link', '../../public/pageSearch/pageSearch')
							changeEditor(_that.autoformdata[0].editors, 'departureCityId', 'linkParam', {
								other: JSON.stringify({
									onlyChinaFlag: true
								}),
								searchfield: 'q',
								namefield: "dicname",
								notefield: "fullName",
								url: "regions/findCountryTreeList",
								cachename: _that.departureCityCacheKey,
								placeholder: '请输入(如:北京/beijing/bj)'
							})
							changeEditor(_that.autoformdata[0].editors, 'costInstId','link', '../../public/pageSearch/pageSearch')
							changeEditor(_that.autoformdata[0].editors, 'costInstId', {
								"linkParam": {
									other: JSON.stringify({
										containBoard: true
									}),
									method: "post",
									namefield: 'instName',
									searchfield: "instName",
									initload: true,
									url: 'tinstitution/findNotBoard1',
									cachename: _that.InstCacheKey,
									placeholder: "请输入部门名称"
								}
							})
							changeEditor(_that.autoformdata[0].editors,"partakeInstId",{
								"link":'../../public/search/search',
								"linkParam":{
									other:JSON.stringify(),
									url:'tinstitution/getComboTree',
									namefield:'text',
									initload:true,
									cachename:_that.partakeInstCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"feesAggregatedWayCode",{
								"linkParam":{
									other:JSON.stringify({
										code:'40346'
									}),
									url:'tsysdict/getComboTree',
									namefield:'text',
									initload:true,
									key:'id',
									cachename:_that.feesAggregatedWayCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"partakeUserId",{
								"link":'../../public/search/search',
								"linkParam":{
									other:JSON.stringify(),
									url:'orgInstPosUserOfficial/getUserListByInstPosId',
									namefield:'text',
									initload:true,
									cachename:_that.partakeUserCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"researchDevelopmentId",{
								"link":'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId:''
									}),
									url:'createProjectShow/findCreateProjectShowPage',
									namefield:"projectName",
									notefield:"projectCode",
									extrafield:"typeName",
									searchfield:'q',
									initload:true,
									cachename:_that.researchDevelopmentCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"businessOpportunityId",{
								"link":'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId:''
									}),
									url:'biddingProject/getBidding',
									namefield:'biddingProjectName',
									notefield:'biddingCode',
									searchfield:'q',
									initload:true,
									cachename:_that.businessOpportunityCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"marketingPlanningProductId",{
								"link":'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										firstId:''
									}),
									url:'inventoryCategory/getExistingProductLineAssortment',
									namefield:'name',
									initload:true,
									cachename:_that.marketingPlanningProductCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"saleOrderId",{
								"link":'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId:''
									}),
									url:'SaleOrderReviewShow/loadDataGrid',
									namefield:'customerNameX',
									notefield:'customerRequirementCode',
									searchfield:'q',
									initload:true,
									cachename:_that.saleOrderCache
								}
							})
							changeEditor(_that.autoformdata[0].editors,"newProductId",{
								"link":'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId:'',
										likeFlag:false
									}),
									url:'productShowSecond/getProductPage',
									namefield:'newProductName',
									notefield:'newProductCode',
									initload:true,
									cachename:_that.newProductCache
								}
							})
							changeEditor(_that.autoformdata[0].editors, 'sameOutUserId', {
								'link':'../../public/searchUser/searchUser',
								linkParam: {
									namefield: urlUtil.userPageUrl().namefield,
									searchfield: "q",
									defaultDataCache:_that.defaultDataCache,
									cachename: _that.sameOutUserCacheKey,
									isNew:true,
									placeholder: urlUtil.userPageUrl().prompt,
									pagination: true
								}
							});
							if(_that.formdata.sameOutUserId){
								let idArr = _that.formdata.sameOutUserId.split(",")
								let nameArr = _that.formdata.sameOutUserName.split(",")
								let userList = []
								for(let index in idArr){
									let obj = {
										id:idArr[index],
										username:nameArr[index]
									}
									userList.push(obj)
								}
								uni.setStorage({
									key:_that.defaultDataCache,
									data:userList
								})
							}
							if(_that.formdata.feesAggregatedWayIds){
								let fawiArr = _that.formdata.feesAggregatedWayIds.split(",")
								let fawnArr = _that.formdata.feesAggregatedWayName.split(",")
								let feesAggregatedWayList = []
								for(let index in fawiArr){
									let obj = {
										id:fawiArr[index],
										text:fawnArr[index]
									}
									feesAggregatedWayList.push(obj)
								}
								changeEditor(_that.autoformdata[0].editors, 'feesAggregatedWayCode', {
									initSelect: feesAggregatedWayList
								})
							}
							if(_that.formdata.affairTypeId){
								_that.oldAffairType = _that.formdata.affairTypeId
							}
							_that.$nextTick(function(){
								_that.initload()
							})
						}
					})
				}
			})
		},
		onUnload(){
			let _that = this
			uni.removeStorage({
				key:_that.defaultDataCache
			})
			
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.addressCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							_that.$refs.autoform.$refs.areaOutsidepop.close()
							formdata.addressId = d.id
							formdata.addressName = d.dicname
							uni.removeStorage({
								key: _that.addressCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.departureCityCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							_that.$refs.autoform.$refs.areaOutsidepop.close()
							formdata.departureCityId = d.id
							formdata.departureCityName = d.dicname
							uni.removeStorage({
								key: _that.departureCityCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.InstCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata; 
							formdata.costInstId = d.id
							formdata.costInstName = d.instname
							uni.removeStorage({
								key: _that.InstCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.partakeInstCache, //费用承担机构
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.partakeInstId = d.id
							formdata.partakeInstName = d.text
						}
						uni.removeStorage({
							key: _that.partakeInstCache
						});
					},
				})
				uni.getStorage({
					key: _that.feesAggregatedWayCache, 
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							let codes =  d.map(e=> e.attributes.code).join(',')
							let names =  d.map(e=> e.text).join(',')
							let ids = d.map(e=> e.id).join(',')
							formdata.feesAggregatedWayCode = codes
							formdata.feesAggregatedWayIds = ids
							formdata.feesAggregatedWayName = names
							_that.$refs.autoform.changeEditor('workOfficeInfo','feesAggregatedWayCode', {
								initSelect: d
							})
						}
						uni.removeStorage({
							key:_that.feesAggregatedWayCache
						})
					},
				})
				uni.getStorage({
					key:_that.partakeUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.partakeUserId = d.id
							formdata.partakeUserName = d.username
						}
						uni.removeStorage({
							key:_that.partakeUserCache
						})
					}
				})
				uni.getStorage({
					key:_that.researchDevelopmentCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.researchDevelopmentId = d.id
							formdata.researchDevelopmentName = d.projectName
							formdata.researchDevelopmentCode = d.projectCode
						}
						uni.removeStorage({
							key:_that.researchDevelopmentCache
						})
					}
				})
				uni.getStorage({
					key:_that.businessOpportunityCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.businessOpportunityId = d.id
							formdata.businessOpportunityName = d.biddingProjectName
							formdata.businessOpportunityCode = d.biddingCode
						}
						uni.removeStorage({
							key:_that.businessOpportunityCache
						})
					}
				})
				uni.getStorage({
					key:_that.marketingPlanningProductCache,
					success:(res)=>{
						if(res.data){
							let d = res.data 
							let formdata = _that.$refs.autoform.formdata
							formdata.marketingPlanningProductId = d.id
							formdata.marketingPlanningProductName = d.name
						}
						uni.removeStorage({
							key:_that.marketingPlanningProductCache
						})
					}
				})
				uni.getStorage({
					key:_that.saleOrderCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.saleOrderId = d.customerRequirementCode
							formdata.saleOrderCode = d.customerRequirementCode
						}
						uni.removeStorage({
							key:_that.saleOrderCache
						})
					}
				})
				uni.getStorage({
					key:_that.newProductCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductId = d.id
							formdata.newProductCode = d.newProductCode
						}
						uni.removeStorage({
							key:_that.newProductCache
						})
					}
				})
				uni.getStorage({
					key: _that.sameOutUserCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key:_that.defaultDataCache,
								data:d
							})
							var formdata = _that.$refs.autoform.formdata;
							let idList = []
							let nameList = []
							let sameOutUserId = ''
							if(formdata.sameOutUserId){
								sameOutUserId = formdata.sameOutUserId.split(',')
							}
							for(let index in d){
								idList.push(d[index].id)
								nameList.push(d[index].username)
							}
							let idStr = idList.map(e =>e).join(",")
							let nameStr = nameList.map(e =>e).join(",");
							formdata.sameOutUserId = idStr
							formdata.sameOutUserName = nameStr
							uni.removeStorage({
								key: _that.sameOutUserCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
</style>
